package handler

import (
	"sep22-hw/model"

	"github.com/gin-gonic/gin"
)

func UserRole(c *gin.Context) {
	var userRole []model.Role

	// select * from roles left join
	// user_roles on user_roles.rid = roles.id

	model.DB.Table("roles").
		Joins("left join user_roles on user_roles.rid = roles.id").
		Find(&userRole).
		Debug()

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表获取成功",
		"data": userRole,
	})
	return
}

func UserRoleSelect(c *gin.Context) {
	id := c.Query("id")

	var userRole model.UserRole
	err := model.DB.Where("id=?", id).Find(&userRole).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": userRole,
	})
	return
}
